$(function () {
    tchpterFun(); //目录展开与折叠

    var courseTypeKey = $("#courseTypeKey").val();
    var courseId =0;
    if(courseTypeKey=='PACKAGE'){//如果是套餐课程，则获取选择框下的第一个课程的目录
        courseId = $("#package-course").val();
    }else{//如果不是套餐课程，直接获取课程的目录数据
        courseId = $("#courseId").val();
    }
    //页面加载完成，第一次加载目录
    queryCourseCatalog(courseId);

    $('body').on('change','#package-course',function () {//改变选择的套餐课程，会重新加载目录
        courseId = $(this).val();
        queryCourseCatalog(courseId);
        //获取课程的学习数据
        getUserStudyCountData(courseId);
        //加载课程下的班级列表
        getCourseClassesList(courseId);
    }).on('click', '.play-catalog-box', function () { //播放章节
        var _courseId = $(this).data('course-id');//要播放的课程的ID，可能是普通课程的ID或者是套餐下课程的ID
        var catalogId = $(this).data('id');//目录ID
        if(courseTypeKey=='PACKAGE'){
            var packageId = $("#_courseId").val();
            toPlayCourse (packageId,_courseId, catalogId);
        }else{
            toPlayCourse (0,_courseId, catalogId);
        }
        return false;
    });

    /**
     * 跳转播放页面
     */
    function toPlayCourse(packageId,courseId, catalogId) {
        window.open(baseloaction + '/web/uc/play/toPlayCourse?packageId=' + packageId + '&courseId='+courseId+'&catalogId=' + catalogId+'&type=PERSON','_blank');
    }

    /**
     * 加载课程目录
     * @param courseId 课程ID
     */
    function queryCourseCatalog(courseId) {
        $.ajax({
            url:baseloaction + '/web/uc/course/queryCourseCatalog',
            type:'post',
            dataType:'text',
            data:{
                'buyCourseId':$("#_courseId").val(),
                'courseId':courseId
            },
            beforeSend:function (XHR) {
                var loadText = '<div class="text-center pt10 pb10"><span class="loading"></span></div>';
                $("#courseDirectory").html(loadText);
                return true;
            },
            success:function (result) {
                $("#courseDirectory").html(result);
            },
            error:function (error) {
                icModale(0,'','系统错误，课程目录加载失败');
            }
        });
    }

    //获取课程的学习数据
    getUserStudyCountData(courseId);

    //加载数据
    getCourseClassesList(courseId);
    /**
     * 获取学习时间统计
     * @param courseId 课程ID
     */
    function getUserStudyCountData(_courseId) {
        $.ajax({
            url: baseloaction +'/web/uc/course/getUserStudyCountData',
            type:'post',
            dataType:'json',
            data:{
                'courseId':_courseId
            },
            success:function (result) {
                if(result.success==true){
                    var obj = result.entity;
                    if(obj !=null){
                        $("#studySumTime").text(obj.studySumTime+'分钟');
                        $($("#studySumTime").parent().parent('section')).attr('data-original-title','累计观看:'+obj.studySumTime+'分钟')
                        $("#weekStudTime").text(obj.weekStudTime+'分钟');
                        $($("#weekStudTime").parent().parent('section')).attr('data-original-title','本周学习:'+obj.weekStudTime+'分钟')

                        var studyCourseRank = obj.studyCourseRank;
                        if(studyCourseRank <=0){
                            $("#studyCourseRank").html('<span>暂无排名</span>');
                        }else{
                            $("#studyCourseRank").html(' <span class="fs18 c-333"><span>'+studyCourseRank+'</span></span>');
                        }

                        var rankOneUser = obj.rankOneUser;
                        if(rankOneUser!=null){
                            $("#rankOneUserName").text(rankOneUser.nickname);
                            $("#rankOneUserImg").attr('src',rankOneUser.avatar);
                        }
                    }
                }
            }
        });
    }

    /**
     * 获取课程下的班列表
     * @param courseId 课程ID
     */
    function getCourseClassesList(courseId) {
        setTimeout(function () {
            $("#course-classes-list-box").html('<section class="no-data__wrap"><span class="no-data-ico"></span><p class="mt20"><span class="c-666">还没有课程相关班级~~</span></p></section>');
            $.ajax({
                url:baseloaction +'/web/uc/course/getCourseToClass',
                type:'post',
                dataType:'json',
                data:{
                    'courseId':courseId
                },
                success:function (result) {
                    if(result.success==true){
                        var list = result.entity;
                        if(list !=null && list.length > 0){
                            var context = '';
                            $.each(list,function (index,clasess) {
                                context += '<div class="pt10 mb20 pb10 mt5">';
                                context += '    <aside class="media-left vam">';
                                context += '        <span class="portrait show">';
                                context += '            <img src="'+clasess.imageMap.pcUrlMap.medium+'">';
                                context +='         </span>';
                                context +='     </aside>';
                                context +='     <section class="media-body ml20">';
                                context +='         <div class="hLh20 txtOf mb5">';
                                context +='             <span class="fs16 c-333">'+clasess.name+'</span>';
                                context +='         </div>';
                                context +='         <span class="fs12 c-666 show hLh20"><span>'+clasess.topicCount+'</span>话题·<span>'+clasess.studentCount+'</span>同学</span>';
                                context +='     </section>';
                                context +='     <div class="media-right vam">';
                                context +='         <a href="'+baseloaction+'/web/classes/info?id='+clasess.id+'" class=" fs18 c-ccc " target="_blank"><i class="fa fa-arrow-circle-o-right"></i></a>';
                                context +='     </div>';
                                context +='</div>';
                            });
                            $("#course-classes-list-box").html(context);
                            lazyCourseImage();
                        }
                    }
                }
            });
        },1000);
    }
});